﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    LeaveDay
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
     <%: Html.Kendo().Grid<VnResource.DataContract.Models.LeaveDayViewModel>()
        .Name("gridLeaveDay")
        .Events(events => events.Change("Grid_OnRowSelect"))
        .Columns(columns =>
        {
            columns.Bound(p => p.HreProfile.ProfileName).Title("Tên NV").Width(200);
            columns.Bound(p => p.HreProfile.CodeEmp).Title("Ma NV").Width(100);
            columns.Bound(p => p.DateStart).Format("{0:dd/MM/yyyy}").Title("Ngày bắt đầu").Width(100);
            columns.Bound(p => p.DateEnd).Format("{0:dd/MM/yyyy}").Title("Ngày kết thúc").Width(100);
            columns.Bound(p => p.LeaveDayType).Width(100).Title("Loại ngày nghỉ");
          //  columns.Command(command => { command.Custom("Edit").Text("Sửa").Click("onEdit"); command.Destroy(); }).Width(160);
        })
        .ToolBar(toolbar => toolbar.Custom().Text("Tạo mới").HtmlAttributes(new { id = "customCommand" , IsPostBack= false }))
        .ToolBar(toolbar => toolbar.Custom().Text("Sửa").HtmlAttributes(new { id = "customCommandModify", IsPostBack = false }))
        .ToolBar(toolbar => toolbar.Custom().Text("Xoá").HtmlAttributes(new { id = "customCommandDelete", IsPostBack = false }))
        .Editable(editable => editable.Mode(GridEditMode.PopUp))
        .Pageable()
        .Sortable()
        .Scrollable()
        .Selectable(selectable => selectable
        .Mode(GridSelectionMode.Single)
         .Type(GridSelectionType.Row))  
        .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Events(events => events.Error("error_handler"))
            .Model(model => model.Id(p => p.ID))
                   .Create(update => update.Action("EditingPopup_Create", "HRM"))
                   .Read(read => read.Action("EditingPopup_Read", "HRM"))
                   .Update(update => update.Action("EditingPopup_Update", "HRM"))
                   .Destroy(update => update.Action("EditingPopup_Destroy", "HRM"))
        )
    %>

    <%
    Html.Kendo().Window()
        .Name("window")
        .Visible(false)
        .Animation(animation =>
        {
            animation.Open(open =>
            {
                open.Zoom(ZoomDirection.In);
            });
            
            animation.Close(close =>
            {
                close.Reverse(true);
                close.Zoom(ZoomDirection.Out);
                close.Reverse(false);                  
                                 
            });
        })
        .Content(() => 
        {
            Html.RenderPartial("UC_LeaveDay");
            %>

            <%--<div style="text-align: center;">
                    <img src="<%=Url.Content("~/Content/web/window/egg-chair.png")%>" alt="ARNE JACOBSEN EGG CHAIR" />
                    <p>ARNE JACOBSEN EGG CHAIR<br /> Image by: <a href="http://www.conranshop.co.uk/" title="http://www.conranshop.co.uk/">http://www.conranshop.co.uk/</a></p>
                </div>    --%>    
            <%
        })
        .Width(600)
        
        .Draggable()
        .Resizable()
        .Title("Ngày nghỉ")
        .Events(events=> events.Close("close"))
        .Render();
%>
    <script type="text/javascript">
        var currentRowID = "";
        
        function error_handler(e) {
            if (e.errors) {
                var message = "Errors:\n";
                $.each(e.errors, function (key, value) {
                    if ('errors' in value) {
                        $.each(value.errors, function () {
                            message += this + "\n";
                        });
                    }
                });
                alert(message);
            }
        }


        $("#customCommandModify").click(function (e) {
            VnrUtils.log("mở màn hình modify");
           
            e.preventDefault();
            var ID = currentRowID;  //new Array(currentDataItem.ID);
          
            Ajax.getJson("HRM", "GetInfoToUpDate", { IDStr: ID }, function (result) {                                
                ProfileID = result.Profile.ProfileID;             
                $("#ProfileID").data("kendoMultiSelect").value(result.Profile.ProfileID);
                $("#DateStart").data("kendoDatePicker").value(result.Profile.DateStart);
                $("#DateEnd").data("kendoDatePicker").value(result.Profile.DateEnd);
                $("#UserApproveID2").data("kendoComboBox").value(result.Profile.UserApproveID2);
                $("#UserApproveID").data("kendoComboBox").value(result.Profile.UserApproveID);
                $("#LeaveDayType").data("kendoComboBox").value(result.Profile.LeaveDayType);
                $('input[name=ID]').val(currentRowID);
            });
                       

            $("#window").data("kendoWindow").open();
            $("#window").data("kendoWindow").center();            
        });

        $("#customCommandDelete").click(function (e) {
            e.preventDefault();
            var IDStr = currentRowID;  //new Array(currentDataItem.ID);
            // var ProfileID;


            $.ajax({
                url: '/HRM/EditingPopup_Destroy',
                type: 'POST',
                async: false,
                data: { ID: IDStr },
                success: function (result) {
                    window.location.reload();
                }
            });


        });

        $("#customCommand").click(function (e) {
            e.preventDefault();
            $("#window").data("kendoWindow").open();
            $("#window").data("kendoWindow").center();
            $("#undo").fadeOut(300);


            $("#ProfileID").data("kendoMultiSelect").value("");
            $("#DateStart").data("kendoDatePicker").value("");
            $("#DateEnd").data("kendoDatePicker").value("");
            $("#UserApproveID2").data("kendoComboBox").value("");
            $("#UserApproveID").data("kendoComboBox").value("");
            $("#LeaveDayType").data("kendoComboBox").value("");
            $('input[name=ID]').val("");

            //put custom logic here
        });
        function close() {
            $("#undo").fadeIn(300);
        }

        $("#undo")
            .bind("click", function () {
                $("#window").data("kendoWindow").open();
                $("#undo").fadeOut(300);
            });


            function onEdit(e) {
                VnrUtils.log("edit");
            //  e.preventDefault();
            var currentDataItem = $("#gridLeaveDay").data("kendoGrid").dataItem($(e.currentTarget).closest("tr"));

            var ID = currentDataItem.ID;  //new Array(currentDataItem.ID);
            // var ProfileID;

           

            $.ajax({
                url: '/HRM/GetInfoToUpDate',
                type: 'POST',
                async: false,
                data: { IDStr: ID },
                success: function (result) {
                    ProfileID = result.Profile.ProfileID;
                    //  alert(result.Profile.LeaveDayType);
                    //   WorkDayFormat = result.WorkDayFormat;
                    //  RegisterHour = result.RegisterHour;

                    $("#ProfileID").data("kendoMultiSelect").value(result.Profile.ProfileID);
                    $("#DateStart").data("kendoDatePicker").value(result.Profile.DateStart);
                    $("#DateEnd").data("kendoDatePicker").value(result.Profile.DateEnd);
                    $("#UserApproveID2").data("kendoComboBox").value(result.Profile.UserApproveID2);
                    $("#UserApproveID").data("kendoComboBox").value(result.Profile.UserApproveID);
                    $("#LeaveDayType").data("kendoComboBox").value(result.Profile.LeaveDayType);
                    $('input[name=ID]').val(currentDataItem.ID);

                }
            });
    
            $("#window").data("kendoWindow").open();
            $("#window").data("kendoWindow").center();
            $("#undo").fadeOut(300);

        }


        Grid_OnRowSelect = function (e) {
            var data = this.dataItem(this.select());
            YourRowID = data.id; //IMP
            currentRowID = YourRowID;
        };

    </script>


</asp:Content>
